/*
 * JID.java
 * Created on Apr 13, 2005 3:33:35 PM
 */

/**
 * @author Andry
 * The JID.java class provides
 */
package edu.mobbuzz.messaging;
public class JID {
    public String username;
    public String domain;
    public String resource;
    
    
    /**
     * @param username
     * @param domain
     * @param resource
     */
    public JID(String username, String domain, String resource) {
        this.username = username;
        this.domain = domain;
        this.resource = resource;
    }
    
    public static JID parseJIDString(String jid) {
        String username = "";
        String domain = "";
        String resource = "";
        int length = jid.length();
        int idx;
        int prev = 0;
        
        if ((idx = jid.indexOf("@")) != -1) {
            username = jid.substring(0, jid.indexOf("@"));
        }
        prev = idx + 1;
        if ((idx = jid.indexOf("/")) == -1) {
            domain 	 = jid.substring(prev);
        } else {
            domain	 = jid.substring(prev, idx);
            resource = jid.substring(idx + 1);
        }
        return (new JID(username, domain, resource));        
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        StringBuffer output = new StringBuffer();
        if (!username.equals("")) {
            output.append(username + "@");
        }
        output.append(domain);
        if (!resource.equals("")) {
            output.append("/").append(resource);
        }
        return output.toString();
    }
    
    /**
     * @return JID without resouce element, only username@domain
     */
    public String toStringWithoutResource() {
        StringBuffer output = new StringBuffer();
        if (!username.equals("")) {
            output.append(username + "@");
        }
        return output.append(domain).toString();
    }
}
